<?php

/**
 * This model class represents the business logic associated with a "room" 
 * model.  While its easy to say that models are generally derived from 
 * database tables, this is not always the case.  Data sources for models are 
 * commonly web services, the filesystem, caching systems, and more.  That 
 * said, for the purposes of this ac2 applicaiton, we have split the 
 * buisness logic from its datasource (the dbTable).
 *
 * This particular class follows the Table Module pattern.  There are other 
 * patterns you might want to employ when modeling for your application, but 
 * for the purposes of this example application, this is the best choice.
 * To understand different Modeling Paradigms: 
 * 
 * @see http://martinfowler.com/eaaCatalog/tableModule.html [Table Module]
 * @see http://martinfowler.com/eaaCatalog/ [See Domain Logic Patterns and Data Source Arch. Patterns]
 */
 
require_once APPLICATION_PATH . '/models/DbTable/RoomZone.php';
require_once APPLICATION_PATH . '/models/DbTable/Setpoint.php';
 
class Model_Room
{
    /** Model_Table_Room */
    protected $_table;

    /**
     * Retrieve table object
     * 
     * @return Model_Room_Table
     */
    public function getTable()
    {
        if (null === $this->_table) {
            // since the dbTable is not a library item but an application item,
            // we must require it to use it
            require_once APPLICATION_PATH . '/models/DbTable/Room.php';
            $this->_table = new Model_DbTable_Room;
        }
        return $this->_table;
    }

    /**
     * Save a new room
     * 
     * @param  array $data 
     * @return int|string
     */
    public function save(array $data)
    {
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);
            }
        }
        return $table->insert($data);
    }

    /**
     * Fetch all entries
     * 
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchEntries()
    {
          $rooms = $this->getTable()->fetchAll('1');
          $result  = array();
          $i = 0;
          foreach ($rooms as $room) {
            $result[$i]['name']    = $room->name;
            $result[$i]['room_id'] = $room->room_id;
            // find setpoint
            $setpoint = $room->findDependentRowset("Model_DbTable_Setpoint");
            $result[$i]['setpoint']  = $setpoint->current()->setpoint;
            // find zone
            $zones = $room->findDependentRowset("Model_DbTable_RoomZone");
            foreach ($zones as $zone) {
              $result[$i]['zone_ids'][] = $zone->zone_id;
            }
            $i++;
          }
          return $result;

    }


    /**
     * Fetch readings
     * 
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchReadings()
    {
      
          require_once APPLICATION_PATH . '/models/Reading.php';
          $readingModel = new Model_Reading;
      
          $rooms = $this->getTable()->fetchAll('1');
          $result  = array();
          $i = 0;
          foreach ($rooms as $room) {
            $result[$i]['name']    = $room->name;
            $result[$i]['room_id'] = $room->room_id;
            // find setpoint
            $setpoint = $room->findDependentRowset("Model_DbTable_Setpoint");
            $result[$i]['setpoint']  = $setpoint->current()->setpoint;
            // find readings
            $zones = $room->findDependentRowset("Model_DbTable_RoomZone");
            foreach ($zones as $zone) {
              $result[$i]['readings'] = $readingModel->fetchbyZoneId($zone->zone_id);
            }
            $i++;
          }          
          return $result;
    }



    /**
     * Fetch an individual entry
     * 
     * @param  int|string $id 
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntry($id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('room_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        return $table->fetchRow($select)->toArray();
    }


    /**
     * Update a new room
     * 
     * @param  array $data 
     * @return int|string
     */
    public function update(array $data)
    {
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);
            }
        }
        $room_id = $data["room_id"];
        $where = "room_id = $room_id";
        unset($data["room_id"]);
        return $table->update($data, $where);
    }

}